package testClasses.misc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import galaxies.CustomGalaxyData;
import galaxies.CustomGalaxyList;
import gameAppLayer.enums.Constants;

import com.thoughtworks.xstream.XStream;

public class DirectoryLoadTest {
	
	//http://www.java-forums.org/new-java/7744-load-all-files-directory.html
	//http://stackoverflow.com/questions/1480398/java-reading-a-file-from-current-directory
	public void execute() throws IOException, ClassNotFoundException {
		XStream xstream = new XStream();
		xstream.alias("customGalaxy", CustomGalaxyData.class);
		
		File dir = new File(Constants.dataPath + "//CustomGalaxies");
		
		
		
		File[] fileList = dir.listFiles();
		
		System.out.println(fileList.length);
		//fileList[0].
		
		for (File file: fileList) {
			System.out.println(file.getName());
			int mid = file.getName().lastIndexOf(".");
			String extension = file.getName().substring(mid+1, file.getName().length());
			System.out.println(extension);
		}
		
		
		FileInputStream fis;
		InputStreamReader isr;
		ObjectInputStream in;
		Object obj;
		CustomGalaxyList galList = new CustomGalaxyList();
		for (int i = 1; i < fileList.length; i++) {
			fis = new FileInputStream(fileList[i]);
			isr = new InputStreamReader(fis);
			in = xstream.createObjectInputStream(isr);
			
			obj = in.readObject();
			CustomGalaxyData data = (CustomGalaxyData) obj;			
			galList.addData(data.getName(), data);			
		}
		
		HashMap<String, CustomGalaxyData> galData = galList.getAllGalaxyData();
		
		for (Map.Entry<String, CustomGalaxyData> entry : galData.entrySet()) {
			System.out.println("Key = " + entry.getKey());
			System.out.println("Object name = " + entry.getValue().getName());
			
			for (ArrayList<Double> point: entry.getValue().getPoints()) {
				System.out.println("(x,y): " + point.get(0) + " " + point.get(1));
			}
			System.out.println();
			
		}
	}
	
	public static void main(String args[]) throws IOException, ClassNotFoundException {
		DirectoryLoadTest dirTest = new DirectoryLoadTest();
		dirTest.execute();
	}
	

}
